﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyCommon.HttpContextUser;
using MyIServices;
using MyModels;
using MyModels.Models;
using MyModels.ViewModels;

namespace MyApi.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class RoleController : BaseApiController
    {
        private readonly IRoleService _roleService;
        private readonly IUser _user;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleService"></param>
        /// <param name="user"></param>
        public RoleController(IRoleService roleService, IUser user,IMapper mapper)
        {
            _roleService = roleService;
            _user = user;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<Role>>> Get(int page = 1, int pageSize = 20, string key = "")
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) key = "";
            var list = await _roleService.QueryPage(i => i.IsDeleted == false && (i.Name != null && i.Name.Contains(key)), page, pageSize, " Id desc ");
            return Success(list, "获取角色列表成功");
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post(Role dto)
        {
            dto.CreateId = _user.UserId;
            dto.CreateBy = _user.UserName;
            var id = await _roleService.Add(dto);
            return id > 0 ? Success(id.ToString(), "添加角色成功") : Fail("添加角色失败");
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put(Role role)
        {
            if (role == null || role.Id <= 0) return Fail("参数错误");
            role.ModifyId = _user.UserId;
            role.ModifyBy = _user.UserName;
            var res = await _roleService.Update(role);
            return res ? Success(role.Id.ToString(), "更新成功") : Fail("更新失败");
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(long id)
        {
            if (id <= 0) return Fail("参数错误");
            var role = await _roleService.QueryById(id);
            if (role == null) return Fail("数据不存在");
            role.IsDeleted = true;
            var res = await _roleService.Update(role);
            return res ? Success(role.Id.ToString(), "删除成功") : Fail("删除失败");
        }
    }
}
